<?php

namespace Wap\Controller;

use User\Model\FactoryModel;
use User\Model\MeetingModel;
use Wap\Model\StatisticsModel;
use Wap\Service\WechatService;
use Wap\Service\WeixinService;

//header('Access-Control-Allow-Origin: *');
class MeetingController extends WapController
{
    public $wxuser;//--活动绑定的微信号--
    public $meet;//--会议--

    public function __construct()
    {
        parent::__construct();
        $this->handleCriteriaWxuser();
        if (empty($this->wxuser)) {
            header("Content-type: text/html; charset=utf-8");
            die("活动不存在！");
        }
        $api = "meeting";
        $id = I('get.meetId', '', '') ? I("get.meetId", -1, "") : I("post.meetId", -1, "");
        StatisticsModel::visit($api, $id);
        $wxJSApi = WeixinService::buildWXJSApi($this->wxuser);
        if (!empty($wxJSApi)) {
            $this->assign('appid', $this->wxuser['appid']);
            $this->assign('nonceStr', $wxJSApi['noncestr']);
            $this->assign('wxTicket', $wxJSApi['ticket']);
            $this->assign('timestamp', $wxJSApi['timestamp']);
            $this->assign('wxSha1', $wxJSApi['sha1']);
            $share_title = $this->meet['name'];
            $share_pic = "http://" . $_SERVER['HTTP_HOST'] . "/Public/images/sun.png";
            $share_desc = "欢迎来到" . $this->meet['name'];
            $share_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            if ($this->meet['has_head']) {
                $share_url = "http://" . $_SERVER['HTTP_HOST'] . "/Wap/Meeting/entry?meetId=" . $id;
            }
            if (!empty($this->meet['share_title'])) {
                $share_title = $this->meet['share_title'];
            }
            if (!empty($this->meet['share_desc'])) {
                $share_desc = $this->meet['share_desc'];
            }
            if (!empty($this->meet['share_pic'])) {
                $share_pic = "http://" . $_SERVER['HTTP_HOST'] . $this->meet['share_pic'];
            }
            if (!empty($this->meet['share_url'])) {
                $share_url = $this->meet['share_url'];
            }
            $this->assign('share_title', $share_title);
            $this->assign('share_desc', $share_desc);
            $this->assign('share_pic', $share_pic);
            $this->assign('share_url', $share_url);
        }
        if (!headers_sent() && // 如果页面头部信息还没有输出--
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展--
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP--
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }

    }

    private function handleCriteriaWxuser()
    {
        $id = I('get.', '', '') ? I("get.meetId", -1, "") : I("post.meetId", -1, "");
        if (!empty($id)) {
            $cond_meet['meet_id'] = $id;
            $cond_meet['status'] = MeetingModel::STATUS_ACTIVE;
            $now_date = date("Y-m-d H:i:s");
            $cond_meet['start_date'] = array("LT", $now_date);
            $cond_meet['end_date'] = array("GT", $now_date);
            $this->meet = M("meeting")->where($cond_meet)->find();
            if (empty($this->meet)) {
                return false;
            }
            $this->assign('share_disable', $this->meet['share_disable']);
            $this->wxuser = session('meeting_wxuser_' . $id);
            if (!empty($this->wxuser)) {
                $this->wxuser = session('meeting_wxuser_' . $id);
                return true;
            }
            $this->wxuser = WeixinService::getWxuser($this->meet['token']);
            if (empty($this->wxuser)) {
                return false;
            }
            session('meeting_wxuser_' . $id, $this->wxuser);
            return true;
        } else {
            return false;
        }
    }

    private function handleCriteriaURL()
    {
        $id = I("get.meetId", -1, "");
        $openid = session('session_meeting_openid_' . $id);
        if (empty($openid)) {
            if (!isset($_GET['code'])) {
                $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                $wechat = new WechatService($this->wxuser['appid'], $this->wxuser['appsecret']);
                $url = $wechat->snsapiUserInfoLink($url, $this->wxuser['appid']);
                header("Location: " . $url);
                exit();
            } else {
                $wechat = new WechatService($this->wxuser['appid'], $this->wxuser['appsecret']);
                $out = $wechat->snsapiUserInfo($this->wxuser['appid'], $this->wxuser['appsecret'], $_GET['code']);
                if ($out != null) {
                    session('session_meeting_openid_' . $id, $out['openid']);
                    session('session_meeting_wxname_' . $id, $out['nickname']);
                    session('session_meeting_wximg_' . $id, $out['headimgurl']);
                }
            }
        }
    }

    public function entry()
    {
        $meet = $this->meet;
        $this->assign('meet', $meet);
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false) {
            //--非微信浏览器--
        } else {
            $is_wx = true;
            $this->assign("is_wx", $is_wx);
            $this->handleCriteriaURL();
            $cond_has_ticket['meet_id'] = $meet['meet_id'];
            $openid = session('session_meeting_openid_' . $meet['meet_id']);
            $cond_has_ticket['openid'] = empty($openid) ? -1 : $openid;
            $cond_has_ticket['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
            $count = M("meeting_user")->where($cond_has_ticket)->count();
            $this->assign("ticket_count", $count);
        }
        return $this->display();
    }

    public function index()
    {
        $id = I("get.meetId", "", "");
        $meet = $this->meet;
        $this->assign('meet', $meet);
        //--注册字段--
        $is_register = I("get.is_register", "", "");
        $is_wx = false;
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false || $meet['wx_disable'] == MeetingModel::WX_DISABLE) {
            //--非微信浏览器--
        } else {
            $is_wx = true;
        }
        $this->assign("is_wx", $is_wx);
        if ($is_wx || !empty($meet['live'])) {
            $this->handleCriteriaURL();
            $cond_has_ticket['meet_id'] = $id;
            $openid = session('session_meeting_openid_' . $id);
            $cond_has_ticket['openid'] = empty($openid) ? -1 : $openid;
            $cond_has_ticket['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
            $count = M("meeting_user")->where($cond_has_ticket)->count();
            $this->assign("ticket_count", $count);
        }
        if ($is_register) {
            $date = date("Y-m-d H:i:s");
            $register_status = 0;
            if ($meet['register_start_date'] > $date) {
                $register_status = 1;
                $date_string = substr($meet['register_start_date'], 0, 4) . "年" . substr($meet['register_start_date'], 5, 2) .
                    "月" . substr($meet['register_start_date'], 8, 2) . "日" . substr($meet['register_start_date'], 11, 2) . "点" .
                    substr($meet['register_start_date'], 14, 2) . "分";
                $this->assign('date_string', $date_string);
            }
            if ($meet['register_end_date'] < $date) {
                $register_status = 2;
            }
            $this->assign("register_status", $register_status);
            $this->field();
        }

        $img = $meet['bannerurl'];
        $cond_menu['meet_id'] = $id;
        $cond_menu['status'] = MeetingModel::STATUS_ACTIVE;
        $menulist = M("meeting_menu")->where($cond_menu)->order('priority asc')->select();
        $this->assign("menulist", $menulist);
        $is_getticket = I("get.is_getticket", "", "");
        if ($is_getticket) {
            $cond_user['meet_id'] = $id;
            $cond_user['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
            $openid = session('session_meeting_openid_' . $id);
            $cond_user['openid'] = empty($openid) ? -1 : $openid;
            $users = M("meeting_user")->where($cond_user)->select();
            $this->assign("img", $img);
            $this->assign('users', $users);
            return $this->display("index");
        }

        $content = '';
        $menu = '';
        $item_more = 0;
        if (empty($menulist)) {
            $this->assign('img', $img);
            $this->assign('content', $content);
            $this->assign('item_more', $item_more);
            return $this->display("index");
        }
        $menuId = I("get.menuId", "", "");
        if (empty($menuId)) {
            $menuId = $menulist[0]['menu_id'];
            $menu = $menulist[0];
        } else {
            foreach ($menulist as $menulist_value) {
                if ($menulist_value['menu_id'] == $menuId) {
                    $menuId = $menulist_value['menu_id'];
                    $menu = $menulist_value;
                }
            }
        }
        $this->assign('menu', $menu);

        $cond_menu_content['menu_id'] = empty($menuId) ? -1 : $menuId;
        $cond_menu_content['status'] = MeetingModel::STATUS_ACTIVE;
        $content = M("meeting_menu_content")->where($cond_menu_content)->order('priority asc')->limit(10)->select();
        $this->assign('content', $content);
        $count = M("meeting_menu_content")->where($cond_menu_content)->count();
        if ($count > sizeof($content)) {
            $item_more = 1;
        }
        $this->assign('item_more', $item_more);
        if (!empty($menu) && !empty($menu['bannerurl']) && !$is_register) {
            $img = $menu['bannerurl'];
        }
        if (($menu['type'] == MeetingModel::MENU_PIC) && !$is_register) {
            $img = "";
        }
        $this->assign('img', $img);
        return $this->display("index");
    }

    public function ajaxIndex()
    {
        $id = I("get.meetId", "", "");
        $meet = $this->meet;
        $ajax_return['meet'] = $meet;
        //--注册字段--
        $is_register = I("get.is_register", "", "");
        $is_wx = false;
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false) {
            //--非微信浏览器--

        } else {
            $is_wx = true;

        }
        if ($is_wx) {
            $this->handleCriteriaURL();
            $cond_has_ticket['meet_id'] = $id;
            $openid = session('session_meeting_openid_' . $id);
            $cond_has_ticket['openid'] = empty($openid) ? -1 : $openid;
            $cond_has_ticket['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
            $count = M("meeting_user")->where($cond_has_ticket)->count();
            $ajax_return['ticket_count'] = $count;
        }
        $ajax_return['is_wx'] = $is_wx;
        if ($is_register) {
            $date = date("Y-m-d H:i:s");
            $register_status = 0;
            if ($meet['register_start_date'] > $date) {
                $register_status = 1;
                $date_string = substr($meet['register_start_date'], 0, 4) . "年" . substr($meet['register_start_date'], 5, 2) .
                    "月" . substr($meet['register_start_date'], 8, 2) . "日" . substr($meet['register_start_date'], 11, 2) . "点" .
                    substr($meet['register_start_date'], 14, 2) . "分";
                $ajax_return['date_string'] = $date_string;
            }
            if ($meet['register_end_date'] < $date) {
                $register_status = 2;
            }
            $ajax_return['register_status'] = $register_status;
            $field = $this->ajaxField();
            $ajax_return['field'] = $field;
        }

        $img = $meet['bannerurl'];
        $cond_menu['meet_id'] = $id;
        $cond_menu['status'] = MeetingModel::STATUS_ACTIVE;
        $menulist = M("meeting_menu")->where($cond_menu)->order('priority asc')->select();

        $is_getticket = I("get.is_getticket", "", "");
        if ($is_getticket) {
            $cond_user['meet_id'] = $id;
            $cond_user['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
            $openid = session('session_meeting_openid_' . $id);
            $cond_user['openid'] = empty($openid) ? -1 : $openid;
            $users = M("meeting_user")->where($cond_user)->select();
            $ajax_return['users'] = $users;
            $ajax_return['img'] = '';
            $this->ajaxReturn($ajax_return);
        }

        $content = '';
        $menu = '';
        $item_more = 0;
        if (empty($menulist)) {
            $ajax_return['img'] = $img;
            $ajax_return['content'] = $content;
            $ajax_return['item_more'] = $item_more;
            $this->ajaxReturn($ajax_return);
        }
        $menuId = I("get.menuId", "", "");
        if (empty($menuId)) {
            $menuId = $menulist[0]['menu_id'];
            $menu = $menulist[0];
        } else {
            foreach ($menulist as $menulist_value) {
                if ($menulist_value['menu_id'] == $menuId) {
                    $menuId = $menulist_value['menu_id'];
                    $menu = $menulist_value;
                }
            }
        }
        $ajax_return['menu'] = $menu;

        $cond_menu_content['menu_id'] = empty($menuId) ? -1 : $menuId;
        $cond_menu_content['status'] = MeetingModel::STATUS_ACTIVE;
        $content = M("meeting_menu_content")->where($cond_menu_content)->order('priority asc')->limit(10)->select();
        $ajax_return['content'] = $content;
        $count = M("meeting_menu_content")->where($cond_menu_content)->count();
        if ($count > sizeof($content)) {
            $item_more = 1;
        }
        $ajax_return['item_more'] = $item_more;
        if (!empty($menu) && !empty($menu['bannerurl']) && !$is_register) {
            $img = $menu['bannerurl'];
        }
        if ($menu['type'] == MeetingModel::MENU_PIC) {
            $img = "";
        }
        $ajax_return['img'] = $img;
        $this->ajaxReturn($ajax_return);
    }

    public function ajaxContent()
    {
        $page = I('get.page', 0, '');
        $cond_menu_content['menu_id'] = I('get.menuId', 0, '');
        $cond_menu_content['status'] = MeetingModel::STATUS_ACTIVE;
        $start = $page * 10;
        $content = M("meeting_menu_content")->where($cond_menu_content)->order('priority asc')->limit($start, 10)->select();
        return $this->ajaxReturn($content);
    }

    public function ajaxUserstatus()
    {
        $user_id = I('get.user_id', 0, '');
        $cond['user_id'] = $user_id;
        $status = M("meeting_user")->where($cond)->getField("status");
        $data['status'] = '';
        $data['user_id'] = $user_id;
        if ($status == MeetingModel::STATUS_USED) {
            $data['status'] = 'used';
        }
        return $this->ajaxReturn($data);
    }

    public function code()
    {
        $user_id = I("get.user_id", "-1", "");
        Vendor("phpqrcode.phpqrcode");
        $QRcode = new \QRcode();
        $ticket = M("meeting_user")->where(['user_id' => $user_id])->getField('ticket');
        $url = $ticket;
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 4;
        $QRcode::png($url, false, $level, $size);
    }

    public function signin()
    {
        $meet_id = I("get.meetId", -1, "");
        $session_open_id = session("user_login_open_id");
        if (empty($session_open_id)) {
            return $this->redirect("/Wap/Applications/meeting_login");
        }
        $meet = $this->meet;
        $this->assign("meet", $meet);
        $cond['status'] = array("IN", array(MeetingModel::STATUS_ACTIVE, MeetingModel::STATUS_USED));
        $cond['meet_id'] = $meet_id;
        $count = M("meeting_user")->where($cond)->count();

        $cond_unuse['status'] = MeetingModel::STATUS_ACTIVE;
        $cond_unuse['meet_id'] = $meet_id;
        $unuse = M("meeting_user")->where($cond_unuse)->count();

        $cond_used['status'] = MeetingModel::STATUS_USED;
        $cond_used['meet_id'] = $meet_id;
        $used = M("meeting_user")->where($cond_used)->count();

        $this->assign("count", $count);
        $this->assign("unuse", $unuse);
        $this->assign("used", $used);

        $get_unuse = I("get.unuse", 0, '');
        $users = '';
        if (empty($get_unuse)) {
            $users = M("meeting_user")->where($cond_used)->order("user_id asc")->select();
        } else {
            $users = M("meeting_user")->where($cond_unuse)->order("user_id asc")->select();
        }
        $this->assign("users", $users);
        return $this->display();
    }

    public function scan()
    {
        $ticket = I("post.ticket", "", "");
        $meetId = I("post.meetId", "", "");
        $cond['meet_id'] = $meetId;
        $cond['ticket'] = $ticket;
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $user_find = M("meeting_user")->where($cond)->find();
        if (empty($user_find)) {
            $this->ajaxReturn(['code' => -1, 'msg' => '无效的二维码']);
        }
        $data['status'] = MeetingModel::STATUS_USED;
        $data['ticket_date'] = date("Y-m-d H:i:s");
        M("meeting_user")->where(['user_id' => $user_find['user_id']])->save($data);
        $this->ajaxReturn(['code' => 1, 'msg' => '签到成功']);
    }

    private function field()
    {
        $cond['meet_id'] = I("get.meetId", -1, "");
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $fields = M("meeting_register_field")->where($cond)->order("priority asc")->select();
        $this->assign("fields", $fields);
        return true;
    }

    private function ajaxField()
    {
        $cond['meet_id'] = I("get.meetId", -1, "");
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $fields = M("meeting_register_field")->where($cond)->order("priority asc")->select();
        return $fields;
    }

    private function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/meeting/'; // 设置附件上传根目录
        $upload->subName = array("date", "Ymd");
        $upload->saveName = array("uniqid", "meeting");
        // 上传文件
        $info = $upload->upload();
        $img = array();
        if ($info) {
            foreach ($info as $key => $file) {
                $image = new Image();
                $image->open("Public/Uploads/meeting/" . date('Ymd') . "/" . $file['savename']);//生成缩略图
                $image->thumb(102, 142, Image::IMAGE_THUMB_FIXED)->save("Public/Uploads/meeting/" . date('Ymd') . "/thumb" . $file['savename']);
                $img[$key] = "http://" . $_SERVER["HTTP_HOST"] . '/Public/Uploads/meeting/' . date('Ymd') . "/thumb" . $file['savename'];
            }
        }
        return $img;
    }




    public function register()
    {
        $params = I("post.", "", "");

        $cond['meet_id'] = $params['meetId'];
        $cond['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
        $fields = M("meeting_register_field")->where($cond)->order('priority asc')->select();
        $data['meet_id'] = $params['meetId'];
        $querynum = 1;
        $cond_exist = array();
        foreach ($fields as $field) {
            if ($field['is_condition']) {
                if (isset($params[$field['field_id']])) {
                    $data['query_value' . $querynum] = $params[$field['field_id']];
                    $cond_exist['query_value' . $querynum] = $params[$field['field_id']];
                }
                $querynum++;
            }
            if ($field['type'] == MeetingModel::FIELD_MULTIPLE) {
                if (isset($params[$field['field_id']])) {
                    $data['field_value'] .= $field['field_id'] . "," . MeetingModel::FIELD_MULTIPLE . "|" . json_encode($params[$field['field_id']]) . ";";
                } else {
                    $data['field_value'] .= $field['field_id'] . "," . MeetingModel::FIELD_MULTIPLE . "|;";
                }
            } else {
                if ($field['type'] == MeetingModel::FIELD_CITY) {
                    $data['field_value'] .= $field['field_id'] . "|" . $params['province' . $field['field_id']] . "," .
                        $params['city' . $field['field_id']] . "," . $params['distinct' . $field['field_id']] . ";";
                    continue;
                }
                if ($field['type'] == MeetingModel::FIELD_ADDRESS) {
                    $data['field_value'] .= $field['field_id'] . "|" . $params['province' . $field['field_id']] . "," .
                        $params['city' . $field['field_id']] . "," . $params['distinct' . $field['field_id']] .
                        "," . $params['address' . $field['field_id']] . ";";
                    continue;
                }
                if ($field['type'] == MeetingModel::FIELD_RANGE) {
                    $data['field_value'] .= $field['field_id'] . "|" . $params['start' . $field['field_id']] . "-" . $params['end' . $field['field_id']] . ";";
                    continue;
                }
                if ($field['type'] == MeetingModel::FIELD_PIC) {
                    if (!empty($params[$field['field_id']])) {
                        $ajaxImg = FactoryModel::ajaxImgUploadThumb($params[$field['field_id']], "meeting");
                        if ($ajaxImg['code'] != 1) {
                            $this->redirect("/User/Meeting/register?is_register=1&meetId=" . $params['meetId'] . "&registered=-1&note=" . "图片上传失败");
                            return;
                        }
                        $data['field_value'] .= $field['field_id'] . "," . MeetingModel::FIELD_PIC . "|" . $ajaxImg['msg'] . ";";
                    } else {
                        $data['field_value'] .= $field['field_id'] . "," . MeetingModel::FIELD_PIC . "|;";
                    }
                    continue;
                }
                if (isset($params[$field['field_id']])) {
                    $data['field_value'] .= $field['field_id'] . "|" . $params[$field['field_id']] . ";";
                } else {
                    $data['field_value'] .= $field['field_id'] . "|;";
                }
            }
        }
        if(!empty($cond_exist)){
            $cond_exist['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
            $cond_exist['meet_id'] = $params['meetId'];
            $user = M("meeting_user")->where($cond_exist)->find();
            if ($user) {
                $note = "此用户已被注册";
                $this->redirect("/Wap/Meeting/index?is_register=1&meetId=" . $params['meetId'] . "&registered=1&note=" . $note);
            }
        }
        $data['is_bus'] = empty($params['is_bus']) ? 0 : $params['is_bus'];
        $data['bus_person'] = empty($params['bus_person']) ? 0 : $params['bus_person'];
        $data['bus_note'] = empty($params['bus_note']) ? '' : $params['bus_note'];

        $data['is_eat'] = empty($params['is_eat']) ? 0 : $params['is_eat'];
        $data['eat_person'] = empty($params['eat_person']) ? 0 : $params['eat_person'];
        $data['eat_note'] = empty($params['eat_note']) ? '' : $params['eat_note'];

        $data['is_hotel'] = empty($params['is_hotel']) ? 0 : $params['is_hotel'];
        $data['hotel_person'] = empty($params['hotel_person']) ? 0 : $params['hotel_person'];
        $data['hotel_note'] = empty($params['hotel_note']) ? '' : $params['hotel_note'];

        if ($this->meet['need_verify']) {
            $data['status'] = MeetingModel::STATUS_PENDING;
        } else {
            $data['status'] = MeetingModel::STATUS_ACTIVE;
        }
        $data['ticket'] = date("mdi") . rand(1, 100) . substr(md5(date("Y-m-d H:i:s")), 0, 5);
        $openid = session('session_meeting_openid_' . $data['meet_id']);
        $data['openid'] = empty($openid) ? '' : $openid;
        $wx_name = session('session_meeting_wxname_' . $data['meet_id']);
        $data['wx_name'] = empty($wx_name) ? '' : $wx_name;
        $wx_img = session('session_meeting_wximg_' . $data['meet_id']);
        $data['wx_img'] = empty($wx_img) ? '' : $wx_img;

        $data['wish'] = empty($params['wish']) ? '' : $params['wish'];
        M("meeting_user")->add($data);
        if ($this->meet['result_type']) {
            Header("Location: " . $this->meet['result_msg']);
            exit;
        }
        $note = $this->meet['result_msg'];
        $this->redirect("/Wap/Meeting/index?is_register=1&meetId=" . $params['meetId'] . "&registered=1&note=" . $note);
        return;
    }

    public function end_sign()
    {
        $cond['meet_id'] = I("get.meetId", -1, "");
        $data['status'] = MeetingModel::STATUS_USED;
        M("meeting_sign")->where($cond)->save($data);
    }

    public function sign()
    {
        $cond['meet_id'] = I("get.meetId", "", "");
        $sign = M("meeting_sign")->where($cond)->find();
        $cond_user['meet_id'] = I("get.meetId", "", "");
        $cond_user['status'] = MeetingModel::STATUS_USED;
        $cond_user['show'] = 1;
        $users = M("meeting_user")->where($cond_user)->limit(5)->order("ticket_date desc")->select();
        $this->assign("sign", $sign);
        $this->assign("users", $users);
        return $this->display();
    }

    public function sign_start()
    {
        $cond['meet_id'] = I("get.meetId", -1, "");
        $cond['status'] = MeetingModel::STATUS_USED;
        $count = M("meeting_user")->where($cond)->count();
        if ($count > 0) {
            $this->ajaxReturn(1);
        } else {
            $this->ajaxReturn(0);
        }
    }

    public function sign_update()
    {
        $code = I("post.code", 0, "");
        $cond_sign['meet_id'] = $cond['meet_id'] = I("post.meetId", -1, "");
        $cond['openid'] = array("NEQ", "");
        $cond['status'] = MeetingModel::STATUS_USED;
        $result['code'] = $code;

        $sign = M("meeting_sign")->where($cond_sign)->find();
        if (!empty($sign) && $sign['status'] != MeetingModel::STATUS_ACTIVE) {//--签到活动结束---
            $result['code'] = 50000;
            $cond['show'] = 1;
            $users = M("meeting_user")->where($cond)->select();
            $result['users'] = $users;
            $this->ajaxReturn($result);
        }

        if ($code == 0) {//--封面隐藏--
            $count = M("meeting_user")->where($cond)->count();
            if ($count > 0) {
                $result['code'] = 10000;
                $this->ajaxReturn($result);
            } else {
                $this->ajaxReturn($result);
            }
        }

        if ($code == 10000) {//--显示签到人员--
            $cond['show'] = 0;
            $user = M("meeting_user")->where($cond)->limit(1)->order("ticket_date asc")->find();
            if (!empty($user)) {
                $cond_update['user_id'] = $user['user_id'];
                $data['show'] = 1;
                M("meeting_user")->where($cond_update)->save($data);
                $result['code'] = 20000;
                $result['user'] = $user;
            } else {
                $result['code'] = 10001;
            }
            $this->ajaxReturn($result);
        }

        if ($code == 20000) {//--翻转效果/显示签到人员--
            $cond['show'] = 1;
            $count = M('meeting_user')->where($cond)->count();
            if ($count % 8 == 0) {
                $result['code'] = 30000;
                $users = M("meeting_user")->where($cond)->limit(32)->order("ticket_date desc")->select();
                $result['users'] = $users;
                $this->ajaxReturn($result);
            } else {
                $cond['show'] = 0;
                $user = M("meeting_user")->where($cond)->limit(1)->order("ticket_date asc")->find();
                if (!empty($user)) {
                    $cond_update['user_id'] = $user['user_id'];
                    $data['show'] = 1;
                    M("meeting_user")->where($cond_update)->save($data);
                    $result['user'] = $user;
                } else {
                    $result['code'] = 20001;
                }
                $this->ajaxReturn($result);
            }
        }

        if ($code == 30000) {//---隐藏或继续显示翻转效果--
            $cond['show'] = 0;
            $count = M("meeting_user")->where($cond)->count();
            if ($count > 0) {
                $result['code'] = 40000;
                $this->ajaxReturn($result);
            } else {
                $result['code'] = 30001;
            }
        }

        if ($code == 40000) {
            $cond['show'] = 0;
            $user = M("meeting_user")->where($cond)->limit(1)->order("ticket_date asc")->find();
            if (!empty($user)) {
                $cond_update['user_id'] = $user['user_id'];
                $data['show'] = 1;
                M("meeting_user")->where($cond_update)->save($data);
                $result['user'] = $user;
                $result['code'] = 20000;
            } else {
                $result['code'] = 40001;
            }
            $this->ajaxReturn($result);
        }
    }

    public function lucky()
    {
        $meet_id = I("get.meetId", -1, '');
        $cond_lucky['meet_id'] = $meet_id;
        $lucky = M("meeting_lucky")->where($cond_lucky)->find();
        $this->assign("lucky", $lucky);

        $cond_item['meet_id'] = $meet_id;
        $cond_item['status'] = MeetingModel::STATUS_ACTIVE;
        $items = M("meeting_lucky_item")->where($cond_item)->select();
        $this->assign("items", $items);
        $this->display();
    }

    public function lucky_users()
    {
        $meet_id = I("get.meetId", -1, '');
        $start = I("get.start", 0, '');
        $cond_user['meet_id'] = $meet_id;
        $cond_user['status'] = MeetingModel::STATUS_USED;
        $count = M("meeting_user")->where($cond_user)->count();
        $users = array();
        if($count > $start){
            $users = M("meeting_user")->where($cond_user)->limit($start,36)->select();
        }
        $this->ajaxReturn($users);
    }

    public function get_lucky_person()
    {
        $meet_id = I("get.meetId", -1, '');
        $item_id = I("get.itemId", -1, '');

        $cond_item['item_id'] = $item_id;
        $item = M("meeting_lucky_item")->where($cond_item)->find();

        $cond_lucky_user['item_id'] = $item_id;
        $cond_lucky_user['status'] = MeetingModel::STATUS_ACTIVE;
        $lucky_users = M("meeting_lucky_user")->where($cond_lucky_user)->select();
        if (sizeof($lucky_users) == $item['num']) {
            $user_arr = [];
            foreach ($lucky_users as $lucky_user) {
                $user_arr[] = $lucky_user['user_id'];
            }
            $users = array();
            if (!empty($user_arr)) {
                $cond_lucky_user_select['user_id'] = array("IN", $user_arr);
                $users = M("meeting_user")->where($cond_lucky_user_select)->select();
            }
            $this->ajaxReturn($users);
        }
        $less_num = $item['num'] - sizeof($lucky_users);

        $cond_unlucky['meet_id'] = $meet_id;
        $cond_unlucky['status'] = MeetingModel::STATUS_USED;
        $cond_unlucky['luckyed'] = 0;
        $user_unlucky_count = M("meeting_user")->where($cond_unlucky)->count();
        for ($i = $less_num; $i > 0; $i--) {
            if ($user_unlucky_count > 0) {
                $start = rand(0, $user_unlucky_count - 1);
                $user = M("meeting_user")->where($cond_unlucky)->limit($start, 1)->find();
                $user['luckyed'] = 1;
                $cond_user_save['user_id'] = $user['user_id'];
                M("meeting_user")->where($cond_user_save)->save($user);
                $data['meet_id'] = $meet_id;
                $data['user_id'] = $user['user_id'];
                $data['item_id'] = $item_id;
                M("meeting_lucky_user")->add($data);
                $user_unlucky_count--;
            }
        }
        $cond_user['meet_id'] = $meet_id;
        $cond_user['item_id'] = $item_id;
        $cond_user['status'] = MeetingModel::STATUS_ACTIVE;
        $lucky_users = M("meeting_lucky_user")->where($cond_user)->getField("user_id", true);
        $users = array();
        if (!empty($lucky_users)) {
            $cond_lucky_user_select['user_id'] = array("IN", $lucky_users);
            $users = M("meeting_user")->where($cond_lucky_user_select)->select();
        }
        $this->ajaxReturn($users);
    }
}
